<?php
declare (strict_types=1);

namespace app\admin\service\site\navi;

use app\admin\logic\site\navi\NaviTop as NaviTopLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 顶部导航配置接口
 */
class NaviTop extends BaseService
{
    /**
     * @Inject()
     *
     * @var NaviTopLogic
     */
    protected $naviTopLogic;

    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-29
     *
     * @return   [type]                         [description]
     */
    public function index()
    {
        $data = $this->naviTopLogic->getList()->hidden(['content']);
        // 定义字段
        $keyList = [
            'title'          => ListItem::make()->title('分组名称/标题'),
            'data_type_text' => ListItem::make()->title('类型'),
            'content'        => ListItem::make('custom')->title('内容'),
            'update_time'    => ListItem::make()->title('最后更新时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取对应分组的导航
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-29
     *
     * @param int $pid [description]
     * @return   [type]                             [description]
     */
    public function getChildren(int $pid)
    {
        $data = $this->naviTopLogic->getList([], $pid)->hidden(['content']);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 新建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @return   [type]                         [description]
     */
    public function create(int $pid)
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->naviTopLogic->createNavi($pid, $data);

            return $this->parseToData($res->toArray(), 1, '添加成功');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey         = $this->keyListInit();
        $form->content_options = $this->naviTopLogic->getContentOptions();

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/admin/site/navi/top/' . $pid . '/create';

        return $form->send();
    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-29
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function update(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->naviTopLogic->editNavi($id, $data);

            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = $this->naviTopLogic->where('id', $id)->findOrFail();

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit();

        $form->datas           = $data;
        $form->content_options = $this->naviTopLogic->getContentOptions();

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/site/navi/top/' . $id . '/edit';

        return $form->send();
    }

    protected function keyListInit()
    {
        return [
            'title'     => FormItem::make()->title('导航名称')->required(),
            'data_type' => FormItem::make('radio')->title('导航类型')->options(app(NaviTopLogic::class)->getDataTypeOptions())->required(),
            'content'   => FormItem::make('custom')->title('导航内容')->required(),
        ];
    }

    /**
     * 删除
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-29
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        $this->naviTopLogic->deleteNavi($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 更新头部导航顺序
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/25
     *
     * @param int $id
     * @param int $sort
     * @return \think\Response
     */
    public function updateSort(int $id, int $sort)
    {
        $this->naviTopLogic->updateSort($id, $sort);

        return $this->parseToData([], 1, '更新成功');
    }

    /**
     * 获取跳转内容选项
     *
     * @return \think\Response
     */
    public function contentOptions()
    {
        $options = $this->naviTopLogic->getContentOptions();

        return $this->parseToData($options, 1);
    }
}
